import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:kmxy_base/base/base_page_state.dart';
import 'package:kmxy_base/network/net.dart';
import 'package:kmxy_base/page/setting/setting_page_view.dart';
import 'package:kmxy_base/util/sp_util.dart';
import 'package:kmxy_base/util/string_util.dart';
import 'package:kmxy_base/widget/global_log_btn.dart';
import 'package:package_info_plus/package_info_plus.dart';

abstract class SettingPageState extends BasePageState<SettingPage> {
  String ipCurrent = "";
  String version = "";

  List dataList = [];

  TextEditingController controller = TextEditingController();

  @override
  initData() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    version = packageInfo.version;
    ipCurrent = Net.dio.options.baseUrl;
    dataList = await SpUtil.getList("SERVER_LIST") ?? [];
    setState(() {});
  }

  /// 默认地址
  defaultIp() async {
    setState(() {
      controller.text = Net.DEFAULT_IP;
    });
  }

  /// 确认修改
  sureModify() async {
    if (controller.text.isEmpty) {
      "请填写IP地址".toast();
      return;
    }
    if (!controller.text.contains("http")) {
      controller.text = "http://${controller.text}";
    }
    Net.dio.options.baseUrl = controller.text;
    if (!dataList.contains(controller.text)) {
      dataList.add(controller.text);
    }
    SpUtil.setList("SERVER_LIST", dataList);
    finishPop(context);
  }

  /// 设置log按钮显示隐藏
  setLog() async {
    GlobalLogBtn.isShowLogBtn
        ? GlobalLogBtn.dismiss()
        : GlobalLogBtn.show(context: context);
    setState(() {});
  }

  @override
  destroyData() {}

  cleanAppId() {
    SpUtil.delAppId();
    '清除成功'.toast();
  }

  urlListF() {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return SafeArea(
            child: Container(
              padding: EdgeInsets.all(20.w),
              height: 400.w,
              width: widthScreen,
              child: ListView(
                children: List.generate(
                  dataList.length,
                  (index) => InkWell(
                    onTap: () {
                      setState(() {
                        controller.text = dataList[index];
                      });
                      finishPop(context);
                    },
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(dataList[index],
                            style: TextStyle(fontSize: 16.sp)),
                        const Icon(Icons.keyboard_arrow_right),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
        });
  }
}
